package com.leetcode.util;

import java.util.List;

/**
 * @Description
 * @Date 2021/11/24 17:35
 * @Author ghery
 * @Version V1.0
 */
public class ListNode {
    public int val;
    public ListNode next;
    public ListNode() {}
    public ListNode(int val) { this.val = val; }
    public ListNode(int val, ListNode next) { this.val = val; this.next = next; }

    /**
     * 把数组转化为链表
     * @param arr
     * @return
     */
    public static ListNode getListFormArray(int[] arr){
        if(arr == null || arr.length < 1){
            return null;
        }
        ListNode head = new ListNode(arr[0]);
        ListNode cur = head;
        for (int i = 1; i < arr.length; i++) {
            ListNode newNode = new ListNode(arr[i]);
            cur.next = newNode;
            cur = cur.next;
        }
        return head;
    }

    public static void printListNode(ListNode node){
        StringBuilder res = new StringBuilder();
        ListNode cur = node;
        while(cur != null){
            res.append(cur.val);
            res.append(",");
            cur = cur.next;
        }
        if(res.length() > 0) res.delete(res.length() - 1,res.length());
        System.out.println(res);
    }
}
